<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/9/6
 * Time: 上午8:04
 */

namespace app\api\module;

use app\common\model\DeveloperException;
use app\common\model\Order as OrderModel;
use app\common\model\User;
use app\common\model\UserException;

class Order extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
            return true;
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function exec($interface)
    {
        switch ($interface)
        {
            /**
             * @api {post} / 【1001】会员定单列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName member_order_list
             * @apiGroup 订单【Order】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             * @apiParam (dataGroup) {String} is_evaluate 是否评价（-1：全部 1：没有评价，2：已评价）
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Order",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "page":"1",
             *     "is_evaluate":"1",
             *     "page_size":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} member_order_list 订单列表
             *
             * @apiSuccess (member_order_list-订单列表) {String} title 联盟商名称
             * @apiSuccess (member_order_list-订单列表) {String} add_time 时间
             * @apiSuccess (member_order_list-订单列表) {String} money 现金
             * @apiSuccess (member_order_list-订单列表) {String} coin h币
             * @apiSuccess (member_order_list-订单列表) {String} orderid 订单号
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "order_list": [
             *       {
             *              "id":"订单id",
             *              "title":"咖啡馆",
             *             "add_time":"121212",
             *             "money":"电话",
             *             "coin":"1",
             *             "orderid":"沂蒙大道"
             *       },
             *       {
             *              "id":"订单id",
             *              "title":"咖啡馆",
             *             "add_time":"121212",
             *             "money":"电话",
             *             "coin":"1",
             *             "orderid":"沂蒙大道"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "会员订单列表！"
             * }
             *
             */
            case H_MEMBER_ORDER_LIST:
                return $this->member_order_list();

            /**
             * @api {post} / 【1002】联盟商定单列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName union_order_list
             * @apiGroup 订单【Order】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Order",
             *   "interface": "1002",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "page":"1",
             *     "page_size":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} union_order_list 订单列表
             *
             * @apiSuccess (union_order_list-订单列表) {String} title 联盟商名称
             * @apiSuccess (union_order_list-订单列表) {String} add_time 时间
             * @apiSuccess (union_order_list-订单列表) {String} money 现金
             * @apiSuccess (union_order_list-订单列表) {String} coin h币
             * @apiSuccess (union_order_list-订单列表) {String} orderid 订单号
             * @apiSuccess (union_order_list-订单列表) {String} star 评价星级
             * @apiSuccess (union_order_list-订单列表) {String} content 评价内容
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "order_list": [
             *       {
             *              "id":"订单id",
             *              "title":"咖啡馆",
             *             "add_time":"121212",
             *             "money":"电话",
             *             "star":"1",
             *             "coin":"1",
             *             "content":"1",
             *             "orderid":"沂蒙大道"
             *       },
             *       {
             *              "id":"订单id",
             *              "title":"咖啡馆",
             *             "add_time":"121212",
             *             "money":"电话",
             *             "coin":"1",
             *             "coin":"1",
             *             "content":"1",
             *             "orderid":"沂蒙大道"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "联盟商订单列表！"
             * }
             *
             */
            case H_UNION_ORDER_LIST:
                return $this->union_order_list();
            /**
             * @api {post} / 【1003】订单评价
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName order_comment
             * @apiGroup 订单【Order】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} order_id 订单id
             * @apiParam (dataGroup) {String} star 评论的星
             * @apiParam (dataGroup) {String} content 评论内容
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Order",
             *   "interface": "1003",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "order_id":"1",
             *     "star":"10",
             *     "content":"我的第一条评论"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *   },
             *   "result_code": "0000",
             *   "result_info": "评论成功！"
             * }
             *
             */
            case H_ORDER_COMMENT:
                return $this->order_comment();
            default:
                send_developer_error("模块【活动资源】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    /**
     * 会员订单列表
     * @return array
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\TokenException
     */
    public function member_order_list()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $is_evaluate=$this->essentialParam('is_evaluate');

        /*用户信息*/
        $user=User::userFromToken($access_token);
        /*订单列表*/
       $list= OrderModel::orderList($user,$page,$page_size,1,$is_evaluate);
       $this->insertResponse('list',$list);
       return $this->response('会员订单列表');
    }

    /**
     * 联盟商订单列表
     * @return array
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\TokenException
     */
    public function union_order_list()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');

        /*用户信息*/
        $user=User::userFromToken($access_token);
        /*订单列表*/
        $list= OrderModel::orderList($user,$page,$page_size,2);
        $this->insertResponse('list',$list);
        return $this->response('联盟商订单列表');
    }

    /**
     * @return array
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function order_comment()
    {
        $access_token=$this->essentialParam('access_token');
        $orderid=$this->essentialParam('order_id');
        $star_num=$this->essentialParam('star');
        $content=$this->essentialParam('content');

        /*用户信息*/
        $user=User::userFromToken($access_token);
        /*订单评价*/
        OrderModel::orderComment($user,$orderid,$star_num,$content);
        return $this->response('订单评论成功！');
    }

}